﻿using System;
using System.Collections.Generic;
using System.Text;
using Autofac;
using IceDog.DNC.Program.Autofac.Helpers;
using IceDog.DNC.Program.Autofac.Model;
using IceDog.DNC.Program.Autofac.Repository;
using IceDog.DNC.Program.Autofac.Services;

namespace IceDog.DNC.Program.Autofac
{
    public class RegisterConfig
    {
        /*
         * // 创建你的builder
         * var builder = new ContainerBuilder();
         * // 通常你只关心这个接口的一个实现
         * builder.RegisterType<SomeType>().As<IService>();
         * // 当然，如果你想要全部的服务（不常用），可以这么写：
         * builder.RegisterType<SomeType>().AsSelf().As<IService>();
         */

        public static IContainer RegisterComponents()
        {
            //创建一个容器
            var builder = new ContainerBuilder();
            //注册单个组件
            //builder.RegisterInstance(new TaskRepository()).As<ITaskRepository>();
            //builder.RegisterType<AuthRepository>().AsSelf().InstancePerDependency();
            //builder.RegisterType<ColorRepository>().AsSelf().InstancePerDependency();

            builder.RegisterType<StringHelper>();
            builder.RegisterType<ConsoleOutputService>().As<IOutputService>();
            //builder.RegisterType<TodayWriterService>().As<IDateWriterService>();
            builder.RegisterType<FullDateWriterService>().As<IDateWriterService>();
            builder.RegisterInstance(new Book("DDD"));//单实例

            //通过程序集生成组件

            /*var assembly = typeof(RegisterConfig).Assembly;
            builder.RegisterAssemblyTypes(assembly)
                .Where(t => t.Name == "Book")
                .AsImplementedInterfaces();
                */
            var container = builder.Build();
            return container;
        }
    }
}
